// 文件选择器相关功能

// 文件夹路径选择器
function createFolderSelector(targetInputId) {
    // 先关闭可能存在的旧弹窗
    closeFolderDialog();
    
    const currentPath = document.getElementById(targetInputId).value;
    
    // 创建自定义对话框
    const modal = document.createElement('div');
    modal.className = 'modal-overlay';
    modal.id = 'folderSelectorModal'; // 添加唯一ID
    modal.style.display = 'flex';
    modal.innerHTML = `
        <div class="modal-container" style="max-width: 500px;">
            <div class="modal-header">
                <div class="modal-title">📂 选择保存路径</div>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 15px; color: #555; line-height: 1.5;">
                    请输入您想要保存文件的完整路径
                </div>
                <div class="form-group">
                    <label class="form-label">文件夹路径</label>
                    <input type="text" class="form-input" id="folderPathInput" value="${currentPath}" 
                           placeholder="例如: D:/downloads/videos">
                </div>
                <div style="font-size: 12px; color: #888; margin-top: 8px;">
                    💡 提示：请确保路径存在或程序有权限创建该路径
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary" onclick="closeFolderDialog()">取消</button>
                <button class="btn btn-primary" onclick="confirmFolderPath('${targetInputId}')">确认</button>
            </div>
        </div>
    `;
    
    document.body.appendChild(modal);
    setTimeout(() => {
        document.getElementById('folderPathInput').focus();
        document.getElementById('folderPathInput').select();
    }, 100);
    
    // 监听回车键
    document.getElementById('folderPathInput').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            confirmFolderPath(targetInputId);
        }
    });
}

// 确认文件夹路径
function confirmFolderPath(targetInputId) {
    const pathInput = document.getElementById('folderPathInput');
    const newPath = pathInput.value.trim();
    
    if (newPath) {
        document.getElementById(targetInputId).value = newPath;
        closeFolderDialog();
        
        // 显示成功提示
        showToast('✅ 保存路径已更新: ' + newPath, 'success');
    } else {
        showToast('❌ 请输入有效的路径', 'error');
    }
}

// 关闭文件夹选择对话框
function closeFolderDialog() {
    const modal = document.getElementById('folderSelectorModal');
    if (modal) {
        document.body.removeChild(modal);
    }
}

// 浏览路径函数们
function browsePath() {
    createFolderSelector('savePath');
}

function browseVmqPath() {
    createFolderSelector('vmqPath');
}

function browseVmqBatchPath() {
    createFolderSelector('vmqBatchPath');
}

function browseTiefenquanPath() {
    createFolderSelector('tiefenquanPath');
}

function browseTiefenquanVideoPath() {
    createFolderSelector('tiefenquanVideoPath');
}

function browseDefaultPath() {
    createFolderSelector('defaultPath');
}

function browseKuaishouPath() {
    createFolderSelector('kuaishouPath');
}

function browseKuaishouBatchPath() {
    createFolderSelector('kuaishouBatchPath');
}

function browseBilibiliPath() {
    createFolderSelector('bilibiliPath');
}
